package com.wakyde.dao;

import com.wakyde.pojo.Department;
import com.wakyde.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class EmployeeDao {

    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;
    static {
        employees = new HashMap<Integer, Employee>();
        employees.put(101, new Employee(1001, "AA", "1639963386@qq.com", 0, new Department(101, "教学部"),new Date()));
        employees.put(102, new Employee(1002, "AB", "2639963386@qq.com", 0, new Department(102, "运营部"),new Date()));
        employees.put(103, new Employee(1003, "AC", "3639963386@qq.com", 0, new Department(103, "技术部"),new Date()));
        employees.put(104, new Employee(1004, "AD", "4639963386@qq.com", 0, new Department(104, "市场部"),new Date()));
        employees.put(105, new Employee(1005, "AE", "5639963386@qq.com", 0, new Department(105, "后勤部"),new Date()));
    }

    // 获得所有部门信息
    public Collection<Employee> getAllEmployee(){
        return employees.values();
    }

    // 通过id部门
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }

    // 主键自增
    private static Integer initId = 1006;
    // 增加一个员工
    public void save(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    // 删除员工
    public void deleteEmployee(Integer id){
        employees.remove(id);
    }
}
